package com.sllx.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SessionFactoryBuilder implements SessionFactory{
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	//用户保存Connection并保证线程安全。
	private ThreadLocal<Connection> connThreadLocal = new ThreadLocal<>();
	
	
	private  Connection createConnection() {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbyydb","dbyy","dbyy");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		connThreadLocal.set(connection);
		return connection;
	}
	
	public  Connection getConnection(){
		Connection connection = connThreadLocal.get();
		if(connection==null){
			connection = createConnection();
		}
		return connection;
	}
	
	
	
}
